﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using QuickTasks.ViewModels.Data;
using Microsoft.Advertising.Mobile.UI;

namespace QuickTasks
{
    public partial class FavoriteMobile : PhoneApplicationPage
    {
        bool change = false;
        FavoritePeople objFavorite = new FavoritePeople();
        List<string> chec = new List<string>();
        string nav = "";
        public FavoriteMobile()
        {
            InitializeComponent();

          
        }
       
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                if (change == false)
                {
                    IEnumerable<System.Windows.Navigation.JournalEntry> back = NavigationService.BackStack;
                    foreach (var b in back)
                    {
                        if (!b.Source.ToString().Contains("MainPage.xaml"))
                            NavigationService.RemoveBackEntry();
                        break;
                    }
                }
                else
                    NavigationService.RemoveBackEntry();
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom event In FavoriteMobile.xaml page", ex);
            }
            
        }
       
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                lbxMobileNumbers.ItemsSource = objFavorite.LoadFavorites();
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo event In FavoriteMobile.xaml page", ex);
            }
        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (NavigationContext.QueryString.TryGetValue("nav", out nav))
            {
            }
            try
            {
                check();
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded event In FavoriteMobile.xaml page", ex);
            }
        }
        public void check1()
        {
            IEnumerable<DependencyObject> cboxes = GetChildsRecursive(lbxMobileNumbers);

            foreach (DependencyObject obj in cboxes.OfType<CheckBox>())
            {

                Type type = obj.GetType();
                if (type.Name == "CheckBox")
                {
                    CheckBox cb = obj as CheckBox;
                    foreach (string s in chec)
                    {
                        if (cb.Tag != null)
                            if (cb.Tag.ToString() == s)
                                cb.IsChecked = true;
                    }
                }
            }
        }
        public void check()
        {
            chec.Clear();

                if (App.Nmobile != "")
                {
                    chec = retrive(App.Nmobile);
                    check1();
                }
           
           
        }

        public List<string> retrive(string s)
        {
            List<string> str = new List<string>();

            string[] Emails = s.Split(new char[] { ',', ';' });
            foreach (string str1 in Emails)
            {
                str.Add(str1);
            }
            return str;

        }

        public static IEnumerable<DependencyObject> GetChildsRecursive(DependencyObject root)
        {
            List<DependencyObject> elts = new List<DependencyObject>();
            try
            {
                elts.Add(root);

                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
                {
                    elts.AddRange(GetChildsRecursive(VisualTreeHelper.GetChild(root, i)));
                }
            }
            catch (Exception ex)
            {
                throw new FormatException(ex.Message);
            }
            return elts;
        }
       
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            try
            {
                change = true;
                App.Nmobile = "";
                App.Mobile = "";
                IEnumerable<DependencyObject> cboxes = GetChildsRecursive(lbxMobileNumbers);

                foreach (DependencyObject obj in cboxes.OfType<CheckBox>())
                {

                    Type type = obj.GetType();
                    if (type.Name == "CheckBox")
                    {
                        CheckBox cb = obj as CheckBox;
                        if (cb.IsChecked == true)
                        {
                            if (App.Nmobile != "")
                                App.Nmobile = App.Nmobile + ";" + cb.Tag.ToString();
                            else
                                App.Nmobile = cb.Tag.ToString();

                            if (App.Mobile != "")
                                App.Mobile = App.Mobile + ";" + cb.Content.ToString();
                            else
                                App.Mobile = cb.Content.ToString();

                            if (nav == "1")
                                NavigationService.Navigate(new Uri("/Views/Message/SendSms.xaml?type=SMS", UriKind.Relative));
                            else
                                NavigationService.Navigate(new Uri("/Views/Message/AddMessages.xaml?type=SMS", UriKind.Relative));


                        }

                    }

                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in ApplicationBarIconButton_Click event In FavoriteMobile.xaml page", ex);
            }
        }
    }
}